home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / jnos / rw_test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-05  |  10.2 KB  |  300 lines

  1. /* Rewrite file test program
  2.    Specify location of rewrite file
  3.    and your hostname below and compile
  4.  
  5.    Code taken directly from NOS
  6.    Compiles with JN110x5 support code
  7.    bcc rw_test.c
  8. */
  9.  
  10. #include <ctype.h>
  11. #include <stdio.h>
  12. #include "global.h"
  13.  
  14. #define Hostname "wa3dsp.ampr.org"
  15. #define Rewrite "rewrite"
  16.  
  17. char *rewrite_address(char *);
  18.  
  19.  
  20. void
  21. main()
  22. {
  23.  char addr[100];
  24.  printf("\nRewrite File Test Program\n\n");
  25.  while (1==1) {
  26.      printf("Enter address - (^C to Exit) -  ");
  27.      scanf("%s",addr);
  28.      printf("\nResult - %s\n\n",rewrite_address(addr));
  29.   }
  30. }
  31.  
  32.  
  33. static int Star __ARGS((char *s,char *p,char **argv,int single));
  34. static int haveDot __ARGS((register char *c,register int len));
  35.  
  36.  
  37. /* Read the rewrite file for lines where the first word is a regular
  38.  * expression and the second word are rewriting rules. The special
  39.  * character '$' followed by a digit denotes the string that matched
  40.  * a '*' character. The '*' characters are numbered from 1 to 9.
  41.  * Example: the line "*@*.* $2@$1.ampr.org" would rewrite the address
  42.  * "foo@bar.xxx" to "bar@foo.ampr.org".
  43.  * $H is replaced by our hostname, and $$ is an escaped $ character.
  44.  * If the third word on the line has an 'r' character in it, the function
  45.  * will recurse with the new address.
  46.  */
  47. char *
  48. rewrite_address(addr)
  49. char *addr;
  50. {
  51.         char *argv[10], buf[200], *cp, *cp2, *retstr;
  52.         int cnt;
  53.         FILE *fp;
  54.  
  55.         if ((fp = fopen(Rewrite,READ_TEXT)) == NULLFILE){
  56.                 printf("\nFile '%s' not found\n",Rewrite);
  57.                 exit(1);
  58.                 }
  59.  
  60.         memset((char *)argv,0,10*sizeof(char *));
  61.         while(fgets(buf,sizeof(buf),fp) != NULLCHAR) {
  62.                 if(*buf == '#')         /* skip commented lines */
  63.                         continue;
  64.  
  65.         if((cp = strchr(buf,' ')) == NULLCHAR) /* get the first word */
  66.                         if((cp = strchr(buf,'\t')) == NULLCHAR)
  67.                                 continue;
  68.                 *cp = '\0';
  69.                 if((cp2 = strchr(buf,'\t')) != NULLCHAR){
  70.                         *cp = ' ';
  71.                         cp = cp2;
  72.                         *cp = '\0';
  73.                 }
  74.                 if(!wildmat(addr,buf,argv))
  75.                         continue;               /* no match */
  76.                 rip(++cp);
  77.         /* scan past additional whitespaces */
  78.         while (*cp == ' ' || *cp == '\t') ++cp;
  79.         cp2 = retstr = (char *) calloc(1,200);
  80.                 while(*cp != '\0' && *cp != ' ' && *cp != '\t')
  81.                         if(*cp == '$') {
  82.                                 if(isdigit(*(++cp)))
  83.                                         if(argv[*cp - '0'-1] != '\0')
  84.                                                 strcat(cp2,argv[*cp - '0'-1]);
  85.                                 if(*cp == 'h' || *cp == 'H') /* Our hostname */
  86.                                         strcat(cp2,Hostname);
  87.                                 if(*cp == '$')  /* Escaped $ character */
  88.                                         strcat(cp2,"$");
  89.                                 cp2 = retstr + strlen(retstr);
  90.                                 cp++;
  91.                         }
  92.                         else
  93.                                 *cp2++ = *cp++;
  94.                 for(cnt=0; argv[cnt] != NULLCHAR; ++cnt)
  95.                         free(argv[cnt]);
  96.                 fclose(fp);
  97.                 /* If there remains an 'r' character on the line, repeat
  98.                  * everything by recursing.
  99.                  */
  100.                 if(strchr(cp,'r') != NULLCHAR || strchr(cp,'R') != NULLCHAR) {
  101.                         if((cp2 = rewrite_address(retstr)) != NULLCHAR) {
  102.                                 free(retstr);
  103.                                 return cp2;
  104.                         }
  105.                 }
  106.                 return retstr;
  107.         }
  108.         fclose(fp);
  109.         return "NO Match";
  110. }
  111. /*
  112.  * @(#)wildmat.c 1.3 87/11/06   Public Domain.
  113.  *
  114. From: rs@mirror.TMC.COM (Rich Salz)
  115. Newsgroups: net.sources
  116. Subject: Small shell-style pattern matcher
  117. Message-ID: <596@mirror.TMC.COM>
  118. Date: 27 Nov 86 00:06:40 GMT
  119.  
  120. There have been several regular-expression subroutines and one or two
  121. filename-globbing routines in mod.sources.  They handle lots of
  122. complicated patterns.  This small piece of code handles the *?[]\
  123. wildcard characters the way the standard Unix(tm) shells do, with the
  124. addition that "[^.....]" is an inverse character class -- it matches
  125. any character not in the range ".....".  Read the comments for more
  126. info.
  127.  
  128. For my application, I had first ripped off a copy of the "glob" routine
  129. from within the find(1) source, but that code is bad news:  it recurses
  130. on every character in the pattern.  I'm putting this replacement in the
  131. public domain.  It's small, tight, and iterative.  Compile with -DTEST
  132. to get a test driver.  After you're convinced it works, install in
  133. whatever way is appropriate for you.
  134.  
  135. I would like to hear of bugs, but am not interested in additions; if I
  136. were, I'd use the code I mentioned above.
  137. */
  138. /*
  139. **  Do shell-style pattern matching for ?, \, [], and * characters.
  140. **  Might not be robust in face of malformed patterns; e.g., "foo[a-"
  141. **  could cause a segmentation violation.
  142. **
  143. **  Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
  144. */
  145.  
  146. /*
  147.  * Modified 6Nov87 by John Gilmore (hoptoad!gnu) to return a "match"
  148.  * if the pattern is immediately followed by a "/", as well as \0.
  149.  * This matches what "tar" does for matching whole subdirectories.
  150.  *
  151.  * The "*" code could be sped up by only recursing one level instead
  152.  * of two for each trial pattern, perhaps, and not recursing at all
  153.  * if a literal match of the next 2 chars would fail.
  154.  */
  155.  
  156. /* Modified by Anders Klemets to take an array of pointers as an optional
  157.    argument. Each part of the string that matches '*' is returned as a
  158.    null-terminated, malloced string in this array.
  159.  */
  160.  
  161. static int
  162. haveDot(s,len)
  163. register char *s;
  164. register int len;
  165. {
  166.         while (len-- > 0)
  167.                 if (*s++ == '.')
  168.                         return TRUE;
  169.         return FALSE;
  170. }
  171.  
  172. static int
  173. Star(s,p,argv,single)
  174. register char *s;
  175. register char *p;
  176. register char **argv;
  177. int single;
  178. {
  179.         char *cp = s;
  180.         while (wildmat(cp, p, argv) == FALSE)
  181.                 if(*++cp == '\0')
  182.                         return -1;
  183.         if ((single == TRUE) && haveDot(s, cp - s))
  184.                 return -1;
  185.         return cp - s;
  186. }
  187.  
  188. int
  189. wildmat(s,p,argv)
  190. register char *s;
  191. register char *p;
  192. register char **argv;
  193. {
  194.         register int last;
  195.         register int matched;
  196.         register int reverse;
  197.         register int cnt;
  198.         int single;
  199.  
  200.         for(; *p; s++,p++){
  201.                 switch(*p){
  202.                 case '\\':
  203.                         /* Literal match with following character; fall through. */
  204.                         p++;
  205.                 default:
  206.                      /*   if(*s != *p)   */
  207.                      if (tolower(*s) != tolower(*p))
  208.                                 return FALSE;
  209.                         continue;
  210.                 case '?':
  211.                         /* Match anything. */
  212.                         if(*s == '\0')
  213.                                 return FALSE;
  214.                         continue;
  215.                 case '*':
  216.                 case '+':
  217.                         single = (*p == '+');
  218.  
  219.                         /* Trailing star matches everything. */
  220.                         if(argv == NULLCHARP)
  221.                                 return *++p ? 1 + Star(s, p, NULLCHARP, single) : TRUE;
  222.                         if(*++p == '\0'){
  223.                                 cnt = strlen(s);
  224.                                 if ((single == TRUE) && haveDot(s, cnt))
  225.                                         return FALSE;
  226.                         } else {
  227.                                 if((cnt = Star(s, p, argv+1, single)) == -1)
  228.                                         return FALSE;
  229.                         }
  230.                         *argv = malloc(cnt+1);
  231.                         strncpy(*argv,s,cnt);
  232.                         *(*argv + cnt) = '\0';
  233.                         return TRUE;
  234.                 case '[':
  235.                         /* [^....] means inverse character class. */
  236.             reverse = (p[1] == '^' || p[1] == '!') ? TRUE : FALSE;
  237.                         if(reverse)
  238.                                 p++;
  239.                         for(last = 0400, matched = FALSE; *++p && *p != ']'; last = *p){
  240.                                 /* This next line requires a good C compiler. */
  241.                                 if(*p == '-' ? *s <= *++p && *s >= last : *s == *p)
  242.                                         matched = TRUE;
  243.                         }
  244.                         if(matched == reverse)
  245.                                 return FALSE;
  246.                         continue;
  247.                 }
  248.         }
  249.         /* For "tar" use, matches that end at a slash also work. --hoptoad!gnu */
  250.         return *s == '\0' || *s == '/';
  251. }
  252.  
  253.  
  254. #ifdef  TEST
  255. #include <stdio.h>
  256.  
  257. extern char *gets();
  258.  
  259. main()
  260. {
  261.         char pattern[80];
  262.         char text[80];
  263.         char *argv[80], *cp;
  264.         int cnt;
  265.     
  266.         while (TRUE){
  267.                 printf("Enter pattern:  ");
  268.                 if(gets(pattern) == NULL)
  269.                         break;
  270.                 while (TRUE){
  271.                         bzero(argv,80*sizeof(char *));
  272.                         printf("Enter text:  ");
  273.                         if(gets(text) == NULL)
  274.                                 exit(0);
  275.                         if(text[0] == '\0')
  276.                                 /* Blank line; go back and get a new pattern. */
  277.                                 break;
  278.                         printf("      %d\n", wildmat(text, pattern, argv));
  279.                         for(cnt = 0; argv[cnt] != NULLCHAR; ++cnt){
  280.                                 printf("String %d is: '%s'\n",cnt,argv[cnt]);
  281.                                 free(argv[cnt]);
  282.                         }
  283.                 }
  284.         }
  285.         exit(0);
  286. }
  287. #endif  /* TEST */
  288.  
  289. /* replace terminating end of line marker(s) with null */
  290. void
  291. rip(s)
  292. register char *s;
  293. {
  294.         register char *cp;
  295.  
  296.         if((cp = strchr(s,'\n')) != NULLCHAR)
  297.                 *cp = '\0';
  298. }
  299.  
  300.